package com.ding.courier;

import com.ding.common.Constant;
import com.ding.common.Message;
import com.ding.common.MessageChannel;
import com.ding.common.Waiter;

import java.io.IOException;
import java.net.Socket;
import java.util.UUID;
import java.util.logging.Logger;

public class CommanderChannel extends MessageChannel {

    private Logger logger = Logger.getLogger("");

    private String password;

    private CPool cPool;

    public CommanderChannel(Socket socket,String password,CPool cPool) {
        super(socket);
        this.password = password;
        this.cPool = cPool;
    }


    public void startReadCommand() {

            while (true) {

                Message message;
                message = readMsg();

                System.out.println(message);

                if (message == null) {
                    return;
                }

                switch (message.getType()) {
                    case Constant.CLOSE:
                        setAfterCloseStrategy(()->{//修改关闭后策略
                            logger.info(message.getMsg());
                        });
                        close();
                        break;

                    case Constant.NEW_STREAM:

                        if (message.getIntroducerId() == null) {

                            System.out.println("没有引荐者id,无法连接");
                            break;
                        }

                        Socket socket = null;
                        try {
                            socket = new Socket();
                            socket.connect(getSocket().getRemoteSocketAddress());
                        } catch (IOException e) {
                            break;
                        }
                        MessageChannel holder = new MessageChannel(socket);
                        if (holder == null) {
                            break;
                        }
                        String id = UUID.randomUUID().toString();
                        holder.setId(id);
                        holder.setIntroducerId(getId());
                        holder.sendMsg(new Message().type(Constant.NEW_STREAM).introducerId(message.getIntroducerId()).password(password).id(id));
                        System.out.println("成功发送新建流通信的信息");

                        Waiter.TaskHolder taskHolder = Waiter.setTimeout(holder::close, 2000);
                        Message msg = null;
                        msg = holder.readMsg();
                        taskHolder.clear();

                        if (msg.getType().equals(Constant.SUCCESS)) {
                            cPool.addOne(holder);
                        } else {
                            logger.warning(message.getType() + ":" + message.getMsg());
                        }
                        break;
                    case Constant.RECYCLE:

                        if (message.getId() == null) {
                            System.out.println("没有被回收通道的id,结束");
                            break;
                        }

                        logger.info("开始回收通道");
                        message.setType(Constant.RECYCLE_SUCCESS);
                        if (cPool.recycle(message.getId())) {//回收
                            sendMsg(message);
                            logger.info("成功回收通道");
                        } else {
                            System.out.println("回收通道失败");
                        }
                        break;
                }
            }

    }

    public void send(Message message) {
        sendMsg(message);
    }

}
